/**
 * 
 */
Ext.ns("examManage");
examManage.init = function() {
	examManage.isRight = false;// 表单填写是否正确
	examManage.isEdit = false;// 是否进行修改

	examManage.big_Id;// 定义选定的大类型的序列号
	examManage.big_Name;// 定义选定的大类型名称

	examManage.small_Id;// 定义选定的小类型的序列号
	examManage.small_Name;// 定义选定的大类型名称
	examManage.exam_Id;
	examManage.exam_Name;
	examManage.photo_Name;
	examManage.startTime;
	examManage.endTime;
	var examManage_photoPath;
	examManage.sm = new Ext.grid.CheckboxSelectionModel({
				handleMouseDown : Ext.emptyFn
			});
	examManage.cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			examManage.sm, {
				header : '考试名称',
				dataIndex : 'examInfo_Name',
				sortable : true
			}, {
				header : '考试类型',
				dataIndex : 'examInfo_Type.small_Name',
				sortable : true
			}, {
				header : '考试费用(单价：元)',
				dataIndex : 'examInfo_Money',
				sortable : true
			}, {
				header : '考试开场时间',
				dataIndex : 'examInfo_StartTime',
				renderer : Ext.util.Format.dateRenderer('Y年m月d日   h时i分'),
				sortable : true
			}, {
				header : '考试创建时间',
				dataIndex : 'examInfo_CreateTime',
				renderer : Ext.util.Format.dateRenderer('Y年m月d日'),
				sortable : true
			}, {
				header : '考试说明',
				dataIndex : 'examInfo_Comments',
				sortable : true
			}]);

	examManage.store = new Ext.data.JsonStore({
				url : 'center/showexamValue',
				totalProperty : 'total',
				root : 'examValues',
				fields : [{
							name : "examInfo_Name",
							mapping : 'examInfo_Name',
							type : 'string'
						}, {
							name : "examInfo_Id",
							mapping : 'examInfo_Id',
							type : 'string'
						}, {
							name : "examInfo_Type.small_Name",
							mapping : 'examInfo_Type.small_Name',
							type : 'string'
						}, {
							name : "examInfo_Type.small_Id",
							mapping : 'examInfo_Type.small_Id',
							type : 'int'
						}, {
							name : "examInfo_Money",
							mapping : 'examInfo_Money',
							type : 'float'
						}, {
							name : "examInfo_StartTime",
							mapping : 'examInfo_StartTime',
							type : 'string'
						}, {
							name : "examInfo_EndTime",
							mapping : 'examInfo_EndTime',
							type : 'string'
						}, {
							name : "examInfo_CreateTime",
							mapping : 'examInfo_CreateTime',
							type : 'string'
						}, {
							name : "examInfo_Comments",
							mapping : 'examInfo_Comments',
							type : 'string'
						}, {
							name : "examInfo_Photo.photo_Name",
							mapping : 'examInfo_Photo.photo_Name',
							type : 'string'
						}]
			});
	examManage.BigTypes = new Ext.data.JsonStore({
				url : 'center/showBigType.action',
				totalProperty : 'total',
				root : 'bigtypes',
				fields : [{
							name : "big_Id",
							type : 'int'
						}, {
							name : "big_Name",
							type : 'string'
						}]
			});
	examManage.SmallTypes = new Ext.data.JsonStore({
				url : 'center/showSmallType.action',
				totalProperty : 'total',
				root : 'smalltypes',
				baseParams : {
					big_Name : examManage.big_Name
				},
				fields : [{
							name : "small_Id",
							mapping : 'small_Id',
							type : 'int'
						}, {
							name : "small_Name",
							mapping : 'small_Name',
							type : 'string'
						}, {
							name : "small_Date",
							mapping : 'small_Date',
							type : 'date'
						}, {
							name : "small_State",
							mapping : 'small_State',
							type : 'string'
						}]
			});
	examManage.Panel = new Ext.grid.GridPanel({
		deferredRender : true,
		sm : examManage.sm,
		cm : examManage.cm,
		store : examManage.store,
		loadMask : true,
		height : 450,
		viewConfig : {
			columnsText : '选择显示列',
			forceFit : true
		},
		tbar : new Ext.Toolbar(['-', {
					id : 'search_bigType',
					fieldLabel : '考试类型',
					allowBlank : false,
					emptyText : '请选择类型名进行查询',
					blankText : '对不起，请选择考试类型',
					xtype : 'combo',
					mode : 'remote',
					triggerAction : 'all',
					valueField : 'big_Name',
					displayField : 'big_Name',
					pageSize : 8,
					minListWidth : 300,
					store : examManage.BigTypes
				}, '-', {
					id : 'search_smallType',
					fieldLabel : '考试类型细节',
					allowBlank : false,
					emptyText : '请选择类型细节',
					blankText : '对不起，请选择类型细节进行查询',
					name : 'examValue.exam_Type',
					xtype : 'combo',
					mode : 'local',
					triggerAction : 'all',
					valueField : 'small_Name',
					displayField : 'small_Name',
					pageSize : 8,
					minListWidth : 300,
					store : examManage.SmallTypes
				}, '-', {
					text : '增加考试信息',
					handler : function() {
						if (examManage.addWindow.isVisible()) {
							examManage.addWindow.hide(this, function() {

									});
						} else {
							examManage.addWindow.show(this, function() {

									});
						}
					}
				}, '-', {
					text : '删除选中项',
					handler : function() {
						var rows = examManage.Panel.getSelectionModel()
								.getSelections();
						if (rows.length < 1) {
							Ext.Msg.alert("提示", "请选择一个或多个选项！");
							return false;
						} else {
							var exam_Ids = []; // 要删除的id
							Ext.each(rows, function(item) {
										exam_Ids.push(item.data.exam_Id);
									});
							Ext.Ajax.request({
								url : 'center/deleteexamValue',
								method : 'post',
								params : {
									exam_Ids : exam_Ids
								},
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									if (obj.success == false) {
										Ext.Msg.alert("提示", "删除失败");
									} else {
										Ext.Msg.alert("提示", "删除成功");
									}
								},
								failure : function(response) {
								}
							});

						}
					}
				}, "-", {
					text : "刷新",
					handler : function() {
						examManage.store.load({
									params : {
										small_Id : examManage.small_Id,
										start : 0,
										limit : 12
									}
								});
					}
				}]),
		bbar : new Ext.PagingToolbar({
					id : 'examManage.pagetool',
					pageSize : 10,
					store : examManage.store,
					displayInfo : true,
					displayMsg : '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
					emptyMsg : "没有记录"
				}),
		listeners : {
			'rowdblclick' : function(grid, rowindex, event) {
				var record = grid.getStore().getAt(rowindex);
				examManage.editPanel.getForm().loadRecord(record);
				examManage_photoPath = "dateFile/examInfo/"
						+ new String(record.get("examInfo_Type.small_Id"))
						+ "/"
						+ new String(record.get("examInfo_Photo.photo_Name"));
				examManage.exam_Id = record.get("examInfo_Id");
				examManage.startTime = record.get("examInfo_StartTime");
				examManage.endTime = record.get("examInfo_EndTime");
				var image = new Ext.form.TextField({
							inputType : 'image',
							autoCreate : {
								tag : "input",
								type : "image",
								src : examManage_photoPath
							},
							width : 300,
							height : 120
						});
				examManage.editPanel.insert(6, image);
				examManage.editPanel.doLayout();
				examManage.editWindow.on("hide", function() {
							examManage.editPanel.remove(image);
						});
				examManage.isRight = false;
				examManage.isEdit = true;
				if (examManage.editWindow.isVisible()) {
					examManage.editWindow.hide(this, function() {

							});
				} else {
					examManage.editWindow.show(this, function() {

							});
				}
			}
		}
	});
	examManage.addPanel = new Ext.FormPanel({
				defaultType : 'textfield',
				width : 700,
				labelAlign : 'center',
				msgTarget : 'side',
				fileUpload : true,
				constrain : true,
				constrainHeader : true,
				bodyStyle : {
					padding : '20px'
				},
				viewConfig : {
					forceFit : true
				},
				items : [{
							id : 'exam_AddName',
							fieldLabel : '考试名称',
							allowBlank : false,
							blankText : '对不起，请填写考试名称！',
							emptyText : '请填写考试名称',
							minLength : 2,
							minLengthText : '请输入类型名大于两个汉字',
							maxLength : 50,
							maxLengthText : '请输入类型名小于五十个汉字',
							name : 'examValue.examInfo_Name'
						}, {
							id : 'exam_AddBigType',
							fieldLabel : '考试大类型',
							allowBlank : false,
							emptyText : '请选择大类型名',
							blankText : '对不起，请选择大类型',
							xtype : 'combo',
							mode : 'remote',
							triggerAction : 'all',
							valueField : 'big_Name',
							displayField : 'big_Name',
							pageSize : 8,
							minListWidth : 300,
							store : examManage.BigTypes
						}, {
							id : 'exam_AddSmallType',
							fieldLabel : '考试小类型',
							allowBlank : false,
							emptyText : '请选择小类型名',
							blankText : '对不起，请选择小类型',
							xtype : 'combo',
							mode : 'local',
							triggerAction : 'all',
							valueField : 'small_Name',
							displayField : 'small_Name',
							pageSize : 8,
							minListWidth : 300,
							store : examManage.SmallTypes
						}, {
							fieldLabel : '考试费用',
							xtype : 'numberfield',
							allowNegative : false,
							allowDecimals : true,
							minValue : 0,
							decimalPrecision : 2,
							emptyText : '请填写价格',
							allowBlank : false,
							blankText : '对不起，考试费用不能为空',
							name : 'examValue.examInfo_Money'
						}, {
							id : 'exam_StartTime',
							fieldLabel : '考试开场时间',
							xtype : 'datetimefield',
							format : 'Y-m-d H:i:s',
							allowBlank : false,
							emptyText : '请选择开场时间',
							blankText : '对不起，考试开场时间不能为空',
							name : 'examValue.examInfo_StartTime'
						}, {
							id : 'exam_EndTime',
							fieldLabel : '考试结束时间',
							xtype : 'datetimefield',
							emptyText : '请选择结束时间',
							format : 'Y-m-d H:i:s',
							allowBlank : false,
							blankText : '对不起，考试结束时间不能为空',
							name : 'examValue.examInfo_EndTime'
						}, {
							name : 'exam_Photo',
							inputType : 'file',
							allowBlank : false,
							blankText : '对不起，请您上传物品照片，以便于审核！',
							fieldLabel : '照片'
						}, {
							name : 'examValue.examInfo_Comments',
							fieldLabel : '考试说明',
							allowBlank : false,
							blankText : '对不起，考试说明不能为空！',
							emptyText : '请填写考试说明',
							xtype : 'htmleditor'
						}],
				buttonAlign : 'center',
				buttons : [{
					text : '提交',
					handler : function() {
						if (examManage.addPanel.getForm().isValid()) {
							examManage.addPanel.getForm().submit({
										url : 'center/addExamInfo.action',
										method : 'post',
										waitMsg : '请稍等,文件正在上传',
										waitTitle : '提示',
										params : {
											small_Id : examManage.small_Id
										},
										success : function(form, action) {
											Ext.Msg.alert('提示', "增加考试信息成功");
											examManage.addPanel.getForm()
													.reset();
											examManage.isRight = false;
										},
										failure : function(form, action) {
											Ext.Msg.alert('提示', "增加考试信息失败！！！！");
										}
									});
						}
					}
				}, /*
					 * { text : '修改', handler : function() { isRight_smallType =
					 * true; if (isEdit_smallType == true && isRight_smallType ==
					 * true && editPanel_smallType.getForm().isValid()) {
					 * editPanel_smallType.getForm().submit({ url :
					 * 'editSmallType', method : 'post', success :
					 * function(form, action) { Ext.Msg.alert('提示', "修改成功");
					 * editPanel_smallType.getForm().reset();
					 * Ext.getCmp("pagetool_smallType") .doRefresh();
					 * isRight_smallType = false; }, failure : function(form,
					 * action) { Ext.Msg.alert('提示', "修改失败！！！！"); } }); } } }
					 */
				{
					text : '重置',
					handler : function() {
						examManage.addPanel.getForm().reset();
					}

				}]
			});
	examManage.editPanel = new Ext.FormPanel({
		defaultType : 'textfield',
		width : 700,
		labelAlign : 'center',
		msgTarget : 'side',
		fileUpload : true,
		constrain : true,
		constrainHeader : true,
		bodyStyle : {
			padding : '20px'
		},
		viewConfig : {
			forceFit : true
		},
		items : [{
					id : 'examInfo_Name',
					fieldLabel : '考试名称',
					allowBlank : false,
					xtype : 'displayfield',
					name : 'examValue.examInfo_Name'
				}, {
					id : 'examInfo_Type.small_Name',
					fieldLabel : '考试类型',
					xtype : 'displayfield',
					readOnly : true
				}, {
					id : 'examInfo_Money',
					fieldLabel : '考试费用',
					xtype : 'numberfield',
					allowNegative : false,
					allowDecimals : true,
					minValue : 0,
					decimalPrecision : 2,
					emptyText : '请填写价格',
					allowBlank : false,
					blankText : '对不起，考试费用不能为空',
					name : 'examValue.examInfo_Money'
				}, {
					id : 'examInfo_StartTime',
					fieldLabel : '考试开场时间',
					// xtype : 'datetimefield',
					format : 'Y-m-d H:i:s',
					allowBlank : false,
					emptyText : '请选择开场时间',
					blankText : '对不起，考试开场时间不能为空',
					name : 'examValue.examInfo_StartTime'
				}, {
					id : 'examInfo_EndTime',
					fieldLabel : '考试结束时间',
					// xtype : 'datetimefield',
					emptyText : '请选择结束时间',
					format : 'Y-m-d H:i:s',
					allowBlank : false,
					blankText : '对不起，考试结束时间不能为空',
					name : 'examValue.examInfo_EndTime'
				}, {
					id : 'examInfo_Photo',
					name : 'exam_Photo',
					inputType : 'file',
					fieldLabel : '照片'
				}, {
					id : 'examInfo_Comments',
					fieldLabel : '考试说明',
					allowBlank : false,
					blankText : '对不起，考试说明不能为空！',
					emptyText : '请填写考试说明',
					xtype : 'htmleditor',
					height : 100,
					name : 'examValue.examInfo_Comments',
					autoScroll : true
				}],
		buttonAlign : 'center',
		buttons : [{
					text : '修改',
					handler : function() {
						if (examManage.editPanel.getForm().isValid()) {
							examManage.editPanel.getForm().submit({
										url : 'center/editExamValue',
										method : 'post',
										params : {
											small_Id : examManage.small_Id,
											"examValue.examInfo_Id" : examManage.exam_Id
										},
										success : function(form, action) {
											Ext.Msg.alert('提示', "修改考试信息成功");
											examManage.editWindow.hide();
										},
										failure : function(form, action) {
											Ext.Msg.alert('提示', "修改考试信息失败！！！！");
										}
									});
						}
					}
				},

				{
					text : '退出',
					handler : function() {
						examManage.editWindow.hide();
					}

				}]
	});
	examManage.Main = new Ext.Panel({
				title : '考试信息管理',
				id : 'examManagerPanel',
				frame : true,
				deferredRender : true,
				closeAction : 'hide',
				cm : examManage.cm,
				store : examManage.store,
				loadMask : true,
				autoWidth : true,
				// autoHeight : true,
				closable : true,
				layout : 'fit',
				viewConfig : {
					forceFit : true
				},
				items : [examManage.Panel]
			});
	examManage.addWindow = new Ext.Window({
				title : '增加考试信息窗口',
				width : 700,
				autoHeight : true,
				modal : true,
				resizable : false,
				draggable : false,
				closeAction : 'hide',
				animateTarget : 'target',
				items : [examManage.addPanel]
			});
	examManage.editWindow = new Ext.Window({
				title : '修改考试信息窗口',
				region : 'center',
				width : 700,
				autoHeight : true,
				modal : true,
				resizable : false,
				draggable : true,
				closeAction : 'hide',
				animateTarget : 'target',
				items : [examManage.editPanel]
			});
	Ext.getCmp("exam_AddBigType").on('select', function(combo) {
				examManage.big_Name = combo.getValue();
				var index = examManage.BigTypes.find('big_Name', combo
								.getValue());
				examManage.big_Id = examManage.BigTypes.getAt(index)
						.get("big_Id");
				Ext.getCmp("exam_AddSmallType").getEl().dom.value = null;
				examManage.SmallTypes.load({
							params : {
								big_Name : examManage.big_Name,
								start : 0,
								limit : 8
							}
						});
			});
	Ext.getCmp("search_bigType").on('select', function(combo) {
				examManage.big_Name = combo.getValue();
				var index = examManage.BigTypes.find('big_Name', combo
								.getValue());
				examManage.big_Id = examManage.BigTypes.getAt(index)
						.get("big_Id");
				examManage.SmallTypes.load({
							params : {
								big_Name : examManage.big_Name,
								start : 0,
								limit : 8
							}
						});
			});
	Ext.getCmp("exam_AddSmallType").on('select', function(combo) {
		examManage.small_Name = combo.getValue();
		var index = examManage.SmallTypes.find('small_Name', combo.getValue());
		examManage.small_Id = examManage.SmallTypes.getAt(index)
				.get("small_Id");
	});
	Ext.getCmp("search_smallType").on('select', function(combo) {
		examManage.small_Name = combo.getValue();
		var index = examManage.SmallTypes.find('small_Name', combo.getValue());
		examManage.small_Id = examManage.SmallTypes.getAt(index)
				.get("small_Id");
		examManage.store.load({
					params : {
						small_Id : examManage.small_Id,
						start : 0,
						limit : 12
					}
				});
	});
};
Ext.onReady(examManage.init);